20. Quiz: Building Classes and Subclasses (2-3)
Directions:
Create a Bicycle subclass that extends the Vehicle class. The Bicycle subclass should override Vehicle's constructor function by changing the default values for wheels from 4 to 2 and horn from 'beep beep' to 'honk honk'.
Your Code:
Start Quiz:
/*
* Programming Quiz: Building Classes and Subclasses (2-3)
*/
class Vehicle {
constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
this.color = color;
this.wheels = wheels;
this.horn = horn;
}
honkHorn() {
console.log(this.horn);
}
}
// your code goes here
/* tests
const myVehicle = new Vehicle();
myVehicle.honkHorn(); // beep beep
const myBike = new Bicycle();
myBike.honkHorn(); // honk honk
*/
User's Answer:
(Note: The answer done by the user is not guaranteed to be correct)
/*
* Programming Quiz: Building Classes and Subclasses (2-3)
*/
class Vehicle {
constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
this.color = color;
this.wheels = wheels;
this.horn = horn;
}
honkHorn() {
console.log(this.horn);
}
}
// your code goes here
class Bicycle extends Vehicle {
constructor(color, wheels = 2, horn = 'honk honk') {
super(color, wheels, horn);
this.horn = horn;
}
honkHorn() {
super.honkHorn();
}
}
const myVehicle = new Vehicle();
myVehicle.honkHorn(); // beep beep
const myBike = new Bicycle();
myBike.honkHorn(); // honk honk